import gradio as gr
from transformers import pipeline

if __name__ == "__main__":
    # quick start
    # def greet(name, intensity):
    #     return f"Hello " * intensity + name + "!"
    # demo = gr.Interface(
    #     fn=greet,
    #     inputs=["text", "slider"],
    #     outputs=["text"]
    # )

    # button
    # def greet(name):
    #     return f"Hello " + name + "!"
    # with gr.Blocks() as demo:
    #     name = gr.Textbox(label="Name")
    #     output = gr.Textbox(label="Output Box")
    #     greet_btn = gr.Button("Greet")
    #     greet_btn.click(fn=greet, inputs=name, outputs=output, api_name="greet")

    # button with decorators
    # with gr.Blocks() as demo:
    #     name = gr.Textbox(label="Name")
    #     output = gr.Textbox(label="Output Box", interactive=True)
    #     greet_btn = gr.Button("Greet")

    #     @greet_btn.click(inputs=name, outputs=output)
    #     def hello(name):
    #         return f"{name} 欢迎光临!"

    # 文本框输入变化监听器
    # def welcome(name):
    #     return f"Welcome to Gradio, {name}!"
    # with gr.Blocks() as demo:
    #     gr.Markdown(
    #     """
    #     # Hello World!
    #     Start typing below to see the output.
    #     """)
    #     inp = gr.Textbox(placeholder="What is your name?")
    #     out = gr.Textbox()
    #     inp.change(welcome, inp, out)

    # 多数据流
    # def increase(num):
    #     return num + 1
    # with gr.Blocks() as demo:
    #     a = gr.Number(label="a")
    #     b = gr.Number(label="b")
    #     atob = gr.Button("a > b")
    #     btoa = gr.Button("b > a")
    #     atob.click(increase, a, b)
    #     btoa.click(increase, b, a)

    # 多数据流
    # asr = pipeline("automatic-speech-recognition", "facebook/wav2vec2-base-960h")
    # classifier = pipeline("text-classification")
    # def speech_to_text(speech):
    #     text = asr(speech)["text"]
    #     return text
    # def text_to_sentiment(text):
    #     return classifier(text)[0]["label"]

    # demo = gr.Blocks()
    # with demo:
    #     audio_file = gr.Audio(type="filepath")
    #     text = gr.Textbox()
    #     label = gr.Label()

    #     b1 = gr.Button("Recognize Speech")
    #     b2 = gr.Button("Classify Sentiment")

    #     b1.click(speech_to_text, inputs=audio_file, outputs=text)
    #     b2.click(text_to_sentiment, inputs=text, outputs=label)

    # 多输入
    # with gr.Blocks() as demo:
    #     a = gr.Number(label="a")
    #     b = gr.Number(label="b")
    #     with gr.Row():
    #         add_btn = gr.Button("Add")
    #         sub_btn = gr.Button("Subtract")
    #     c = gr.Number(label="sum")

    #     def add(num1, num2):
    #         return num1 + num2
    #     add_btn.click(add, inputs=[a, b], outputs=c)

    #     def sub(data):
    #         return data[a] - data[b]
    #     sub_btn.click(sub, inputs={a, b}, outputs=c)

    # 多输出 列表
    # with gr.Blocks() as demo:
    #     food_box = gr.Number(value=10, label="Food Count")
    #     status_box = gr.Textbox()
    #     def eat(food):
    #         if food > 0:
    #             return food - 1, "full"
    #         else:
    #             return 0, "hungry"
    #     gr.Button("EAT").click(
    #         fn=eat,
    #         inputs=food_box,
    #         outputs=[food_box, status_box]
    #     )

    #多输出 字典
    # with gr.Blocks() as demo:
    #     food_box = gr.Number(value=10, label="Food Count")
    #     status_box = gr.Textbox()
    #     def eat(food):
    #         if food > 0:
    #             return {food_box: food - 1, status_box: "full"}
    #         else:
    #             return {status_box: "hungry"}
    #     gr.Button("EAT").click(
    #         fn=eat,
    #         inputs=food_box,
    #         outputs=[food_box, status_box]
    #     )

    #返回新组件,并修改值
    def change_textbox(choice):
        if choice == "short":
            return gr.Textbox(lines=2, visible=True)
        elif choice == "long":
            return gr.Textbox(lines=8, visible=True, value="Lorem ipsum dolor sit amet")
        else:
            return gr.Textbox(visible=False)

    with gr.Blocks() as demo:
        radio = gr.Radio(
            ["short", "long", "none"], label="What kind of essay would you like to write?"
        )
        text = gr.Textbox(lines=2, interactive=True, show_copy_button=True)
        radio.change(fn=change_textbox, inputs=radio, outputs=text)

    demo.launch()
